//
//  CLBaseTool.m
//  togetherLBS
//
//  Created by louis on 15/7/30.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLBaseTool.h"
#import "CLBaseParam.h"
//#import "MJExtension.h"
#import "CLDeviceIOS.h"

@implementation CLBaseTool

/**
 * 在原有的URL基础上拼接DID
 */
+ (NSString *)url:(NSString*)url AppendingDid:(NSString*) did{
    
   /* NSString *tempUrl = [NSString stringWithFormat:@"%@/%@",url,did];
    if (did == nil ) {
        tempUrl = [NSString stringWithFormat:@"%@/%@",url,@"0"];
    }
    
    return tempUrl;*/
    
    NSString *tempUrl = url;
    return tempUrl;
    
}

+ (void)getWithUrl:(NSString *)url param:(id)param success:(void (^)(id success))success failure:(void (^)(NSError *))failure
{
    [[CLHttpTool sharedHttpTool] getWithUrl:url paramaters:param success:^(id responseObject) {
        
        DLog(@"url:%@\n params:%@\n responseObject: %@", url,param,responseObject);
    
        id result = [NSNumber mj_objectWithKeyValues:responseObject];
        
        if (success) {
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}

+ (void)getWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *params = [param mj_keyValues];
    NSString *tempUrl = [self url:url AppendingDid:[CLDeviceIOS single].device_id];
    [[CLHttpTool sharedHttpTool] getWithUrl:tempUrl paramaters:params success:^(id responseObject) {
        
        DLog(@"url:%@\n params:%@\n responseObject: %@", tempUrl,params,responseObject);
        
        id result = nil;
        if (resultClass) {
            result = [resultClass mj_objectWithKeyValues:responseObject];
        } else {
            result = responseObject;
        }
        
        if (success) {
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)getImageWithUrl:(NSString *)url param:(id)param success:(void(^)(id response))success failure:(void(^)(NSError *error))failure
{
    NSDictionary *params = [param mj_keyValues];
    
    // 请求资源文件，不用拼接did
    [[CLHttpTool sharedHttpTool] getImageWithUrl:url paramaters:params success:^(id responseObject) {
        
        DLog(@"url:%@\n params:%@\n responseObject: %@", url,params,responseObject);
        
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)getVoiceDataWithUrl:(NSString *)url success:(void (^)(NSData *))success failure:(void (^)(NSError *))failure
{
    // 请求资源文件，不用拼接did
    [[CLHttpTool sharedHttpTool] getVoiceDataWithUrl:url success:^(id responseObject) {
        DLog(@"url:%@\n responseObject: %@", url, responseObject);
        
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *params = [param mj_keyValues];
    NSString *tempUrl = [self url:url AppendingDid:[CLDeviceIOS single].device_id];

    [[CLHttpTool sharedHttpTool] postWithUrl:tempUrl paramaters:params success:^(id responseObject) {
        
        DLog(@"url:%@\n params:%@\n responseObject: %@", tempUrl,params,responseObject);
        id result = nil;
        if (resultClass) {
            result = [resultClass mj_objectWithKeyValues:responseObject];
        } else {
            result = responseObject;
        }
        
        if (success) {
            success(result);
        }
    } failure:^(NSError *error) {
        
        DLog(@"url:%@\n params:%@\n", tempUrl,params);
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithUrl:(NSString *)url param:(CLBaseParam *)param fileDatas:(NSArray *)fileDatas resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *params = [param mj_keyValues];
    
    NSString *tempUrl = [self url:url AppendingDid:[CLDeviceIOS single].device_id];
    [[CLHttpTool sharedHttpTool] postWithUrl:tempUrl paramaters:params formDataArray:fileDatas success:^(id responseObject) {
        DLog(@"url:%@\n params:%@\n, responseObject: %@", tempUrl,params,responseObject);
        id result = nil;
        if (resultClass) {
            result = [resultClass mj_objectWithKeyValues:responseObject];
        } else {
            result = responseObject;
        }
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)cancelCurrent
{
    [[CLHttpTool sharedHttpTool] cancelCurrentRequest];
}

+ (void)cancelAll
{
    [[CLHttpTool sharedHttpTool] cancelAllRequest];
}
@end
